<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="css/layui.css" media="all">
</head>
<body>
	<br />
	<form class="layui-form" id="myform" style="width: 90%"
		onsubmit="return false">
		<div class="layui-form-item">
			<label class="layui-form-label">博客类型</label>
			<div class="layui-input-inline" style="width: 25%">
				<select name="tno" id="tno" lay-verify="required">
					<option value="">请选择博客类型</option>
				</select>
			</div>
		</div>
		

		<div class="layui-form-item">
			<label class="layui-form-label">博客标题</label>
			<div class="layui-input-inline" style="width: 25%">
				<input type="text" name="title" lay-verify="required"
					lay-reqtext="博客标题不能为空..." placeholder="请输入博客标题" autocomplete="off"
					class="layui-input" style="width: 400px;" id="title">
			</div>
			<label class="layui-form-label">首页图片</label>
			<div class="layui-input-inline" style="width: 200px;">
				<input type="file" name="photos" id="photos" autocomplete="off"
					onchange="setImagePreviews(this, 'showpic')" multiple="multiple">
			</div>
		</div>
		<div class="layui-form-item" style="margin-left: 20px;" id="showpic"></div>
		<div class="layui-form-item">
			<div class="layui-inline">
				<label class="layui-form-label">博客详细</label>
				<div class="layui-input-block">
					<textarea name="goods_form_descr" id="goods_form_descr" rows="10" cols="80"></textarea>
				</div>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button type="submit" class="layui-btn" lay-submit=""
					lay-filter="demo1">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>

<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
<script type="text/javascript" src="js/layui.all.js" charset="utf-8"></script>
<script type="text/javascript" src="js/ajaxfileupload.js"
		charset="utf-8"></script>
<script type="text/javascript" src="js/showpic.js" charset="utf-8"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script>
CKEDITOR.replace('goods_form_descr');
layui.use(['form'], function(){
	var form = layui.form,
	layer = layui.layer;

	let str = "";
	str += "<option value='"+ 1 +"'>日常点滴</option>";
	str += "<option value='"+ 2 +"'>资源共享</option>";
	str += "<option value='"+ 3 +"'>其他</option>";
	$("#tno").append($(str));
	form.render("select")
	
	// 自定义验证规则
	form.verify({
    	title: function(value){
      		if(value.length < 2){
        		return '博客名称至少得2个字符...';
      		}
    	}
	});
  
	// 监听提交
  	form.on('submit(demo1)', function(data){
  		let obj = data.field;
		let define1 = CKEDITOR.instances.goods_form_descr.getData();
		obj.define1 = define1;

		$.ajaxFileUpload({
			url:"/blog/add",
			secureuri:false,
			fileElementId:"photos",
			data:obj,
			dataType:"json",
			success:function(rt,status){
				if(rt.code == 511){
					layer.msg("输入信息不完整,请确认后提交...");
					return false;
				}

				if(rt.code == 200){
					$("#myform")[0].reset();   //重置表单
					$("#showpic").html("");  //清空图片预览
					CKEDITOR.instances.goods_form_descr.setData("");
					layui.form.render();
					layer.msg("添加成功...",{icon:1,time:2000});
					return false;
				}
				layer.msg("添加博客信息失败...",{icon:2,time:2000});
			},
			error:function(rt,status,e){
				console.log(data)
				layer.msg("添加博客信息失败\n" + e,{icon:2,time:2000})
			}
		})
		return false;
	});
});
</script>
</body>
</html>